home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3
/
CHAPTE25
/
EX22.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-30
|
2KB
|
73 lines
#include <genstub.c>
DWORD TLSIndex = 0;
BOOL fAbort;
// Show progress without invoking waiting thread.
VOID ShowRunningThread( HWND hWnd )
{
HDC hDC = GetDC( hWnd );
TextOut(hDC, 0, 0, TlsGetValue( TLSIndex ), lstrlen( TlsGetValue( TLSIndex ) ) );
ReleaseDC( hWnd, hDC );
}
// Thread procedure. This function just prints a message once every half-second.
DWORD WINAPI ThreadProc( HWND hWnd )
{
LPVOID lpVoid = HeapAlloc( GetProcessHeap(), 0, 128 );
wsprintf( lpVoid, "Instance %8lx is now running. ", GetCurrentThreadId() );
TlsSetValue( TLSIndex, lpVoid );
while ( !fAbort )
{
Sleep( 500 );
ShowRunningThread( hWnd );
Sleep( 0 );
}
HeapFree( GetProcessHeap(), 0, lpVoid );
ExitThread( TRUE );
}
// Windows message procedure.
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
TLSIndex = TlsAlloc();
break;
case WM_DESTROY:
TlsFree( TLSIndex );
PostQuitMessage( 0 );
break;
case WM_COMMAND:
switch ( LOWORD( wParam ) )
{
case IDM_TEST:
{
int i;
HANDLE hThreads[4]; // Array of threads to wait for.
DWORD dwChildID; // Child ID - discarded.
fAbort = FALSE;
for (i=0; i<4; i++)
hThreads[i] = CreateThread( NULL, 0, ThreadProc,
hWnd, 0, &dwChildID );
// Wait half a minute.
WaitForMultipleObjects( 4, &hThreads, TRUE, 28000 );
fAbort = TRUE;
WaitForMultipleObjects( 4, &hThreads, TRUE, 2000 );
// Clean screen.
InvalidateRect( hWnd, NULL, TRUE );
UpdateWindow( hWnd );
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
break;
default:
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
return (NULL);
}